var prefix = jsctx + "/hlhtTenantOpenList";

var positiveHlhtStation = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        },10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });

    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listPagePositiveHlhtStation",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    pageSize: 20,
                    pageList: [20],
                    pageNumber: 1,
                    sidePagination: "server",
                    cache: false,
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            tenantOpenId: $("#tenantOpenId").val().trim(),
                            hlhtOperatorId:$("#hlhtOperatorId").val(),
                            stationName: $("#stationName").val().trim(),
                            cityId: $("#cityId").val().trim(),
                            address: $("#address").val().trim(),
                            operateStatus: $("#operateStatus").val(),
                        }
                    },
                    columns: [
                        {
                            checkbox: true, field: '', width: '50', align: 'center',
                        },
                        {
                            title: '#', field: '', width: '50', align: 'left',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },
                        {field: 'stationName', title: '充电站', align: 'left', width: '100'},
                        {
                            field: '', title: '充电桩', align: 'center', width: '100',
                            formatter: function (value, row, index) {

                                    return '<span>直' + row.directCurrent + '交'+ row.alternatingCurrent + '</span>'

                            }
                        },
                        {field: '', title: '站点状态', width: '100', align: 'left'},
                        {
                            field: 'operateStatus', title: '运营状态', width: '100', align: 'left',
                            formatter: function (value, row, index) {
                                if (value == '2') {
                                    return '<span>下线</span>';
                                } else if (value == '1') {
                                    return '<span >上线</span>';
                                }else{
                                    return '<span >未上线</span>'
                                }
                            }
                        },

                        {
                            title: '操作',
                            field: 'roleId',
                            width: '200', align: 'center',
                            formatter: function (value, row, index) {
                                var a = '<a class="btn btn-warning btn-sm ' + has_edit + '" href="#" title="桩列表" onclick="positiveHlhtStation.editStation('
                                    + row.elecStationId + ')">编辑站点</a> ';
                                var c = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#"  onclick="positiveHlhtStation.updateStationStatus('
                                    + row.elecStationId + ')">上线</a> ';
                                var d = '<a class="btn btn-danger btn-sm ' + has_edit + '" href="#"  onclick="positiveHlhtStation.updateStationStatus('
                                    + row.elecStationId +')">下线</a> ';
                                var e = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#"  onclick="positiveHlhtStation.detailStation('
                                    + row.elecStationId + ')">查看详情</a> ';
                                var g = '<a class="btn btn-secondary btn-sm ' + has_edit + '" href="#"  onclick="positiveHlhtStation.pileList('
                                    + row.elecStationId + ')">桩列表</a> ';
                                if (row.operateStatus == "1"){
                                    return  d + e + a + g ;
                                }else{
                                    return  c + e + a + g;
                                }

                            }
                        }
                    ]
                });
    }

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    }

    function addStationHlhtTenantOpen(elecStationId) {
        location.href = prefix + "/addStationHlhtTenantOpen/" + elecStationId;
    }

    function editStation(elecStationId) {
        location.href = prefix + "/editPositiveHlhtStation/" + elecStationId;
    }

    function stationHlhtTenantOpenListPage(stationHlhtTenantOpenId) {
        location.href = prefix + "/stationHlhtTenantOpenListPage/" + stationHlhtTenantOpenId;
    }

    function detailStation(elecStationId) {
        location.href = prefix + "/positiveHlhtStationDetail/" + elecStationId;
    }

    function pileList(elecStationId) {
        location.href = prefix + "/pileList/" + elecStationId;
    }

    function updateStationStatus(elecStationId) {
        layer.confirm('确定要变更状态？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: jsctx +"/elecStation/updateStatus",
                type: "post",
                data: {
                    'elecStationId': elecStationId
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("变更成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        });
    }

    function deleteHlhtTenantOpenStation(OpenStationId) {
        $.ajax({
            url: prefix + "/deleteHlhtTenantOpenStation",
            type: "post",
            data: {
                'OpenStationId': OpenStationId
            },
            success: function (data) {
                layer.close(index);
                if (data.status == 200) {
                    layer.msg(data.message);
                    reLoad();
                } else {
                    layer.msg(data.message);
                }
            }
        });
    }


    function deleteStationHlhtTenantOpen(priceId) {
        layer.confirm('确定要删除选中的记录？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/deleteStationHlhtTenantOpen",
                type: "post",
                data: {
                    'priceId': priceId
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("删除成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }

    function goBack() {
        window.history.back();
    }


    function batchRows(operateStatus) {
        var rows = $("#exampleTable").bootstrapTable('getSelections');//
        if (rows.length == 0) {
            alert("请先选择要操作的记录!");
            return;
        } else {
            var elecStationIdList = new Array();
            $(rows).each(function () {
                elecStationIdList.push(this.elecStationId);
            });
            batchUpdateOpenStation(elecStationIdList, operateStatus);
        }
    }

    function batchUpdateOpenStation(elecStationIdList, operateStatus) {
        layer.confirm('确定进行批量操作？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/updateBatchPositiveHlhtStation",
                type: "post",
                data: {
                    elecStationIdList: elecStationIdList,
                    operateStatus:operateStatus

                },
                traditional:true,
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg(data.message);
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        });
    }

    function updateOpenStation(openStationId, openStationOperateStatus) {
        $.ajax({
            url: prefix + "/updateOpenStation",
            type: "post",
            data: {
                openStationId: openStationId,
                openStationOperateStatus:openStationOperateStatus

            },
            success: function (data) {
                layer.close(index);
                if (data.status == 200) {
                    layer.msg(data.message);
                    reLoad();
                } else {
                    layer.msg(data.message);
                }
            }
        });
    }



    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        addStationHlhtTenantOpen: addStationHlhtTenantOpen,
        stationHlhtTenantOpenListPage: stationHlhtTenantOpenListPage,
        deleteStationHlhtTenantOpen: deleteStationHlhtTenantOpen,
        editStation: editStation,
        updateStationStatus:updateStationStatus,
        batchRows:batchRows,
        batchUpdateOpenStation:batchUpdateOpenStation,
        updateOpenStation:updateOpenStation,
        detailStation:detailStation,
        pileList:pileList
    };
})();


